/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sales.hancadso.client.presenter.grid.menuitem;

import br.com.sales.hancadso.client.presenter.events.RemoveEditingEvent;
import br.com.sales.hancadso.client.presenter.events.SaveRecordOnClickEvent;
import br.com.sales.hancadso.client.service.HanCadSOService;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import java.util.Date;

public class AdvClickListenerImpl implements AdvClickListener {

    @Inject
    private EventBus eventBus;
    private JSONObject record = new JSONObject();
    DateTimeFormat format = DateTimeFormat.getFormat("dd/MM/yyyy HH:mm:ss");
    private final PopupPanel popupPanel = new PopupPanel(true);
    Scheduler.ScheduledCommand aproveCommand = new Scheduler.ScheduledCommand() {

        @Override
        public void execute() {
            Date date = new Date(System.currentTimeMillis());
            record.put("DT_APROVACAO", new JSONString(format.format(date)));
            eventBus.fireEvent(new SaveRecordOnClickEvent(record));
            service.updateDateRequest("DT_APROVACAO", Long.parseLong(record.get("SEQ").isNumber().toString()), null);
            popupPanel.hide();
        }
    };

    Scheduler.ScheduledCommand finishedCommand = new Scheduler.ScheduledCommand() {

        @Override
        public void execute() {
            Date date = new Date(System.currentTimeMillis());
            record.put("DT_ENCERRAMENTO", new JSONString(format.format(date)));
            eventBus.fireEvent(new SaveRecordOnClickEvent(record));
            service.updateDateRequest("DT_ENCERRAMENTO", Long.parseLong(record.get("SEQ").isNumber().toString()), null);
            popupPanel.hide();
        }
    };

    Scheduler.ScheduledCommand cancelCommand = new Scheduler.ScheduledCommand() {

        @Override
        public void execute() {
            Date date = new Date(System.currentTimeMillis());
            record.put("DT_CANCELAMENTO", new JSONString(format.format(date)));
            eventBus.fireEvent(new SaveRecordOnClickEvent(record));
            service.updateDateRequest("DT_CANCELAMENTO", Long.parseLong(record.get("SEQ").isNumber().toString()), null);
            popupPanel.hide();
        }
    };
    private final MenuBar popupMenuBar = new MenuBar(true);
    private final MenuItem aprove = new MenuItem("Aprovar", true, aproveCommand);
    private final MenuItem finished = new MenuItem("Encerrar", true, finishedCommand);
    private final MenuItem cancel = new MenuItem("Cancelar", true, cancelCommand);
    @Inject
    private HanCadSOService service;

    @Inject
    public AdvClickListenerImpl() {
        createPopupMenu();
    }

    private void createPopupMenu() {
        popupPanel.setStyleName("popup");
        aprove.addStyleName("popup-item");
        cancel.addStyleName("popup-item");
        finished.addStyleName("popup-item");
        popupMenuBar.setVisible(true);
        popupPanel.add(popupMenuBar);
        popupPanel.addCloseHandler(new CloseHandler<PopupPanel>() {

            @Override
            public void onClose(CloseEvent<PopupPanel> event) {
                eventBus.fireEvent(new RemoveEditingEvent());
            }
        });
    }

    @Override
    public void onRightClick(JSONValue sender, Event event) {
        record = sender.isObject();
        int x = DOM.eventGetClientX(event);
        int y = DOM.eventGetClientY(event);
        popupMenuBar.clearItems();
        if (sender.isObject().get("DT_APROVACAO") == null
                && sender.isObject().get("DT_CANCELAMENTO") == null) {
            popupMenuBar.addItem(cancel);
            popupMenuBar.addItem(aprove);
        }
        if (sender.isObject().get("DT_APROVACAO") != null
                && sender.isObject().get("DT_ENCERRAMENTO") == null) {
            popupMenuBar.addItem(finished);
        }
        popupPanel.setPopupPosition(x, y);
        popupPanel.show();
    }
}
